package com.ksd.web.controller.system;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ksd.common.annotation.Log;
import com.ksd.common.core.controller.BaseController;
import com.ksd.common.core.domain.AjaxResult;
import com.ksd.common.core.page.TableDataInfo;
import com.ksd.common.enums.BusinessType;
import com.ksd.common.enums.SourceTypeEnum;
import com.ksd.common.excel.MovieExcel;
import com.ksd.common.utils.StringUtils;
import com.ksd.common.utils.poi.ExcelUtil;
import com.ksd.system.domain.SysApkVersion;
import com.ksd.system.domain.SysBoard;
import com.ksd.system.domain.SysTvData;
import com.ksd.system.service.ISysBoardService;
import com.ksd.system.service.ISysTvDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ksd.system.domain.SysMovie;
import com.ksd.system.service.ISysMovieService;

/**
 * 影视专区Controller
 * 
 * @author y.wu
 * @date 2025-09-05
 */
@RestController
@RequestMapping("/system/sysMovie")
public class SysMovieController extends BaseController
{
    @Autowired
    private ISysMovieService sysMovieService;
    @Autowired
    private ISysTvDataService sysTvDataService;
    @Autowired
    private ISysBoardService sysBoardService;
    /**
     * 查询影视专区列表
     */
    @PreAuthorize("@ss.hasPermi('system:sysMovie:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysMovie sysMovie)
    {
        startPage();
        List<SysMovie> list = sysMovieService.selectSysMovieList(sysMovie);
        SysBoard sysBoard = sysBoardService.selectSysBoardById(SysBoard.serviceFileId);
        String url = sysBoard.getUrl();
        for (SysMovie movie : list) {
            movie.setImgUrl(url + movie.getImgUrl());
            movie.setFileUrl(url + movie.getFileUrl());

            SysTvData sysTvData = sysTvDataService.selectSysTvDataById(movie.getTypeData());
            if (StringUtils.isNotNull(sysTvData)) {
                movie.setMovieType(sysTvData.getName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出影视专区列表
     */
    @PreAuthorize("@ss.hasPermi('system:sysMovie:export')")
    @Log(title = "影视专区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMovie sysMovie)
    {
        List<SysMovie> list = sysMovieService.selectSysMovieList(sysMovie);
        ExcelUtil<SysMovie> util = new ExcelUtil<SysMovie>(SysMovie.class);
        util.exportExcel(response, list, "影视专区数据");
    }
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<MovieExcel> util = new ExcelUtil<>(MovieExcel.class);
        List<SysMovie> movieList = sysMovieService.selectSysMovieList(new SysMovie());
        List<MovieExcel> list = new ArrayList<MovieExcel>();
        SysMovie movie = movieList.get(0);
        if (StringUtils.isNotNull(movie)){
            Long typeData = movie.getTypeData();
            SysTvData sysTvData = sysTvDataService.selectSysTvDataById(typeData);
            MovieExcel sysContentDataExcel = new MovieExcel();
            sysContentDataExcel.setTitle(movie.getTitle());
            sysContentDataExcel.setFileType(sysTvData.getName());
            sysContentDataExcel.setVideoAddress(movie.getVideoAddress());
            sysContentDataExcel.setImgAddress(movie.getImgAddress());
            list.add(sysContentDataExcel);
        }
        util.exportExcel(response,list,"电影模板");
    }

    /**
     * 获取影视专区详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:sysMovie:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysMovieService.selectSysMovieById(id));
    }

    /**
     * 新增影视专区
     */
    @PreAuthorize("@ss.hasPermi('system:sysMovie:add')")
    @Log(title = "影视专区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMovie sysMovie)
    {
        return toAjax(sysMovieService.insertSysMovie(sysMovie));
    }

    /**
     * 修改影视专区
     */
    @PreAuthorize("@ss.hasPermi('system:sysMovie:edit')")
    @Log(title = "影视专区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMovie sysMovie)
    {
        return toAjax(sysMovieService.updateSysMovie(sysMovie));
    }

    /**
     * 删除影视专区
     */
    @PreAuthorize("@ss.hasPermi('system:sysMovie:remove')")
    @Log(title = "影视专区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (Long id : ids) {
            SysMovie movie = sysMovieService.selectSysMovieById(id);
            Integer souceType = movie.getSouceType();
            if (souceType == SourceTypeEnum.synch.getCode() || souceType == SourceTypeEnum.usb.getCode()){
                String videoAddress = movie.getVideoAddress();
                String imgAddress = movie.getImgAddress();
                File movieFile = new File(videoAddress);
                if (movieFile.exists()) {
                    boolean delete = movieFile.delete();
                    logger.info("删除内容同步电影资源:"+delete+",地址:"+movieFile.getAbsolutePath());
                }
                File imgFile = new File(imgAddress);
                if (imgFile.exists()) {
                    boolean delete = imgFile.delete();
                    logger.info("删除内容同步电影海报:"+delete+",地址:"+imgFile.getAbsolutePath());
                }
            }
            logger.info("删除电影数据来源:"+souceType);
            sysMovieService.deleteSysMovieById(id);
        }
        return success();
    }
}
